app.controller('userController', function ($scope, $controller, userService) {

    $controller('baseController', {$scope: $scope});//继承


    //查询所有学生
    $scope.findAll = function () {
        // alert("所有学生")
        userService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }


    //获得用户名
    $scope.showName = function () {
        userService.showName().success(
            function (response) {
                $scope.loginName = response.data;
            }
        );
    }

    //注销
    $scope.logout = function () {
        userService.logout().success(
            function (response) {
                if (response.success) {
                    alert("注销成功");
                    window.location = "/shoplogin.html";
                } else {
                    alert(response.message)
                }
            }
        );
    }

    //用户登录
    $scope.login = function (entity) {
        // alert("登录了");
        userService.login(entity).success(
            function (response) {
                alert(response.message);
                if (response.data.identity == 1) {
                    window.location = "/admin/index.html";
                } else {
                    window.location = "/student/index.html";
                }
            }
        );
    }

    //用户注册
    $scope.regist = function (entity) {
        userService.regist(entity).success(
            function (response) {
                alert(response.message);
                if(response.success){
                    window.location = "/shoplogin.html";
                }
            }
        );
    }


    //保存
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.entity.id != null) {//如果有ID
            serviceObject = userService.update($scope.entity); //修改
        } else {
            serviceObject = userService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                if (response.success) {
                    //重新查询
                    $scope.reloadList();//重新加载
                } else {
                    alert(response.message);
                }
            }
        );
    }

    //查询实体
    $scope.findOne = function (id) {
        userService.findOne(id).success(
            function (response) {
                $scope.entity = response;
            }
        );
    }


    //批量删除
    $scope.dele = function () {
        //获取选中的复选框
        userService.dele($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                    $scope.selectIds = [];
                }
            }
        );
    }

    //搜索
    $scope.search = function () {
        userService.search($scope.searchEntity).success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //用户初始化uid
    $scope.initUser = function () {
        userService.initUser().success(
            function (response) {
                $scope.findOne(response.data);
            }
        );
    }
});